﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Linq
{
    public static class LinqExtend
    {
        public static decimal AverageOrDefault(this IEnumerable<decimal> source)
        {
            if (source == null || !source.Any())
            {
                return 0;
            }
            return source.Average();
        }

        public static decimal AverageOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
        {
            if (source == null || !source.Any())
            {
                return 0;
            }
            return source.Average(selector);
        }

        public static decimal? AverageOrDefault(this IEnumerable<decimal?> source)
        {
            if (source == null || !source.Any())
            {
                return null;
            }
            return source.Average();
        }

        public static decimal? AverageOrDefault<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal?> selector)
        {
            if (source == null || !source.Any())
            {
                return null;
            }
            return source.Average(selector);
        }
    }
}
